package com.example.administrator.himalaya.activity;

import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.view.KeyEvent;
import android.widget.RadioGroup;

import com.example.administrator.himalaya.R;
import com.example.administrator.himalaya.criticallies.DownloadFragment;
import com.example.administrator.himalaya.criticallies.FirstFragment;
import com.example.administrator.himalaya.criticallies.FourthFragment;
import com.example.administrator.himalaya.criticallies.SecondFragment;
import com.example.administrator.himalaya.dialog.CustomDialog;

public class MainActivity extends AppCompatActivity implements RadioGroup.OnCheckedChangeListener {
    private RadioGroup mGroup;
    private FragmentManager mManager;
    private FirstFragment mFirstFragment;
    private SecondFragment mSecondFragment;
    private DownloadFragment mThridFragment;
    private FourthFragment mFourthFragment;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mGroup = (RadioGroup) findViewById(R.id.group);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        mGroup.setOnCheckedChangeListener(this);
        mFirstFragment = new FirstFragment();
        mSecondFragment = new SecondFragment();
        mThridFragment = new DownloadFragment();
        mFourthFragment = new FourthFragment();
        mManager = getSupportFragmentManager();
        FragmentTransaction tx = mManager.beginTransaction();
        tx.add(R.id.content, mFirstFragment);
        tx.commit();
    }

    @Override
    public void onCheckedChanged(RadioGroup radioGroup, int checkedId) {
        FragmentTransaction tx = mManager.beginTransaction();
        switch (checkedId) {
            case R.id.first:
                tx.replace(R.id.content, mFirstFragment);
                break;
            case R.id.second:
                tx.replace(R.id.content, mSecondFragment);
                break;
            case R.id.thrid:
                tx.replace(R.id.content, mThridFragment);
                break;
            case R.id.fourth:
                tx.replace(R.id.content, mFourthFragment);
        }
        tx.commit();
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
//        if (keyCode == KeyEvent.KEYCODE_BACK
//                && event.getRepeatCount() == 0) {
//            //do something...
//            final AlertDialog.Builder builder = new AlertDialog.Builder(this);
//            builder.setTitle("温馨提示");
//            builder.setMessage("确定要退出喜马拉雅FM?");
//
//            //确定按钮
//            builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
//                @Override
//                public void onClick(DialogInterface dialogInterface, int i) {
//                    //结束应用
//                    android.os.Process.killProcess(android.os.Process.myPid());
//                }
//            });
//            //否定按钮
//            builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
//                @Override
//                public void onClick(DialogInterface dialogInterface, int i) {
//                }
//            });
//            //中立按钮
//            builder.setNeutralButton("最小化", new DialogInterface.OnClickListener() {
//                @Override
//                public void onClick(DialogInterface dialogInterface, int i) {
//                    //实现后台播放,service
//                }
//            });
//            builder.setCancelable(false);
//            AlertDialog dialog = builder.create();
        CustomDialog dialog = new CustomDialog(this);
        dialog.showDialog();
        return true;
    }

}
